package net.zoneland.zrdp.framework.web.service.uniflow.impl;


import net.zoneland.zrdp.framework.web.service.uniflow.WorkIdService;
import net.zoneland.zrdp.common.core.redis.RedisService;
import net.zoneland.zrdp.common.utils.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 工单号业务实现.
 * @version v1.0
 */
@Service
public class WorkIdServiceImpl implements WorkIdService {

    @Resource
    private RedisService redisService;

    /**
     * 获取当前工单编号：格式demo202101270001.
     * @param appName 应用标识
     * @return java.lang.String
     **/
    @Override
    public String getWorkId(final String appName) {
        final StringBuilder workIdBuilder = new StringBuilder();
        //应用编码+时间蹉
        if (StringUtils.isNotBlank(appName)) {
            workIdBuilder.append(appName);
        }
        workIdBuilder.append(DateUtil.getDateYMD());
        //工单号长度为4为，不足补零
        final String seqVal = redisService.getNextSequenceStr();
        workIdBuilder.append(seqVal);
        return workIdBuilder.toString();
    }

    /**
     * 重置当天工单序号.
     **/
    @Override
    public void resetWorkId() {
        //设置初始值0
        redisService.setSequenceZero();

    }
}
